package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Result<Map<String, Object>> queryByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页插件，并进行查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        List<Category> list = categoryMapper.queryByPage(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        //将查询的结果封装到map中并返回
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("records",list);
        return Result.success(map);
    }

    /**
     * 分类修改
     * @param categoryDTO
     * @return
     */
    @Override
    public Result updateByCategory(CategoryDTO categoryDTO) {
        //创建实体类对象并复制属性
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //设置更新时间和更新人
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        //调用mapper进行修改
        categoryMapper.update(category);
        //返回
        return Result.success();
    }

    /**
     * 分类启用禁用
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result updateByStatus(Integer status, Integer id) {
        //创建实体类对象，并为对象设置需要修改的值
        Category category = new Category();
        category.setId(Long.valueOf(id));
        category.setStatus(status);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        //调用mapper进行修改
        categoryMapper.update(category);
        return Result.success();
    }

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @Override
    public Result insertCategory(CategoryDTO categoryDTO) {
        //创建实体类对象并属性拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //设置初始值
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        //调用mapper进行添加并返回结果
        categoryMapper.insert(category);
        return Result.success();
    }

    /**
     * 分类删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Long id) {
        //查询该分类下是否有菜品或套餐关联
        int dishCount = dishMapper.queryByCategoryId(id);
        int setmealCount = setmealMapper.queryByCategoryId(id);
        if(dishCount >0 || setmealCount >0){
            throw new DeletionNotAllowedException("该分类下有菜品或套餐关联，不能删除！");
        }
        //调用mapper进行删除
        categoryMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 分类根据类别查询
     * @param type
     * @return
     */
    @Override
    public Result<List<Category>> queryByType(Integer type) {
        List<Category> list = categoryMapper.queryByType(type);
        ArrayList<Category> result = new ArrayList<>();
        list.forEach(category -> {
            if(category.getStatus() == StatusConstant.ENABLE){
                result.add(category);
            }
        });
        return Result.success(result);
    }

    /**
     * 分类列表查询
     * @param type
     * @return
     */
    @Override
    public Result<List<Category>> list(Integer type) {
        //判断是否启售
        List<Category> categories = categoryMapper.queryByType(type);
        ArrayList<Category> result = new ArrayList<>();
        categories.forEach(category -> {
            if(category.getStatus() == StatusConstant.ENABLE){
                result.add(category);
            }
        });
        return Result.success(result);
    }


}
